package com.cmbdb.job;

        import org.apache.http.HttpEntity;
        import org.apache.http.HttpHost;
        import org.apache.http.HttpStatus;
        import org.apache.http.auth.AuthScope;
        import org.apache.http.auth.UsernamePasswordCredentials;
        import org.apache.http.client.AuthCache;
        import org.apache.http.client.ClientProtocolException;
        import org.apache.http.client.CredentialsProvider;
        import org.apache.http.client.config.RequestConfig;
        import org.apache.http.client.methods.CloseableHttpResponse;
        import org.apache.http.client.methods.HttpGet;
        import org.apache.http.client.protocol.HttpClientContext;
        import org.apache.http.config.Registry;
        import org.apache.http.config.RegistryBuilder;
        import org.apache.http.conn.socket.ConnectionSocketFactory;
        import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
        import org.apache.http.conn.socket.PlainConnectionSocketFactory;
        import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
        import org.apache.http.conn.ssl.SSLContexts;
        import org.apache.http.conn.ssl.TrustStrategy;
        import org.apache.http.impl.auth.BasicScheme;
        import org.apache.http.impl.client.*;
        import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
        import org.apache.http.util.EntityUtils;
        import org.apache.poi.hssf.usermodel.HSSFCell;
        import org.apache.poi.hssf.usermodel.HSSFRow;
        import org.apache.poi.hssf.usermodel.HSSFSheet;
        import org.apache.poi.hssf.usermodel.HSSFWorkbook;

        import javax.net.ssl.*;
        import javax.xml.crypto.Data;
        import java.io.*;
        import java.security.*;
        import java.security.cert.CertificateException;
        import java.security.cert.X509Certificate;
        import java.text.DateFormat;
        import java.text.SimpleDateFormat;
        import java.util.ArrayList;
        import java.util.Arrays;
        import java.util.Date;
        import java.util.List;
        import java.util.regex.Matcher;
        import java.util.regex.Pattern;


/**
 * Created by Dell on 2017/2/8.
 * 用于http、https协议调接口
 */
public class GetTrafficData {

    public String get(String apiUrl, String hostUrl, int port, String protocol, String usernmae, String passWorld) throws Exception {
        //创建默认的httpclient实例

        CloseableHttpClient httpclient = null;

        HttpEntity httpEntity = null;
        String returnValue = null;
        try {
            httpclient = HttpClients.createDefault();
            HttpHost targetHost = new HttpHost(hostUrl, port, protocol);
            //创建参数队列
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(
                    new AuthScope(targetHost.getHostName(), targetHost.getPort()),
                    new UsernamePasswordCredentials(usernmae, passWorld));
            //AuthCache authCache = new BasicAuthCache();
            //BasicScheme basicScheme = new BasicScheme();
            //authCache.put(targetHost, basicScheme);

            HttpClientContext context = HttpClientContext.create();
            context.setCredentialsProvider(credsProvider);
            //context.setAuthCache(authCache);

            String url = protocol + "://" + hostUrl + ":" + port + apiUrl;

            HttpGet httpget = new HttpGet(url);
            //设置请求和传输超时时间
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(30000).setConnectTimeout(30000).build();

            httpget.setConfig(requestConfig);

            CloseableHttpResponse response = httpclient.execute(httpget);
            httpEntity = response.getEntity();
            if (httpEntity != null) {
                returnValue = EntityUtils.toString(httpEntity, "UTF-8");
            }
            EntityUtils.consume(httpEntity);
            response.close();
            // System.out.println("返回内容==="+EntityUtils.toString(httpEntity,"UTF-8"));

        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            httpclient.close();
        }

        returnValue = regMatch(returnValue,"<table.*?>[\\s\\S]*<\\/table>");

        return returnValue;
    }

    public String regMatch(String withinText, String regString) {
        String code = null;
        Pattern pattern = Pattern.compile(regString);
        Matcher matcher = pattern.matcher(withinText);
        if (matcher.find()) {
            code = matcher.group(0);
        }
        return code;
    }

    public void getTrafficExcel() throws Exception {
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建HSSFSheet对象
        HSSFSheet sheet = wb.createSheet("sheet1");

        HSSFRow title = sheet.createRow(0);

        HSSFCell title1 = title.createCell(0);

        title1.setCellValue("区域名称");

        HSSFCell title2 = title.createCell(1);

        title2.setCellValue("交通指数");

        HSSFCell title3 = title.createCell(2);

        title3.setCellValue("平均车速");

        HSSFCell title4 = title.createCell(3);

        title4.setCellValue("拥堵等级");

        int temp = 0;

        for(int i = 0; i < 3; i ++){

            String trafficDataTable = "";
            try {
                trafficDataTable = get("/sectcongmore.aspx?page=" + (i + 1), "202.104.124.215", 80, "http", "", "");
            }catch (Exception ex){
                System.out.println("网络连接中断！！！");
                Thread.sleep(30000);
            }

            String regString = "<tr.*?>[\\s\\S]*?" +
                    "<td.*?>[\\s]*?([\\s\\S]*?)[\\s]*?<\\/td>[\\s\\S]*?" +
                    "<td.*?>[\\s]*?([\\s\\S]*?)[\\s]*?<\\/td>[\\s\\S]*?" +
                    "<td.*?>[\\s]*?([\\s\\S]*?)[\\s]*?<\\/td>[\\s\\S]*?" +
                    "<td.*?>*?[\\s]*?<span.*?>[\\s]*?([\\s\\S]*?)[\\s]*?<\\/span>[\\s]*?<\\/td>[\\s\\S]*?" +
                    "<\\/tr>";

            Pattern pattern = Pattern.compile(regString);
            Matcher matcher = pattern.matcher(trafficDataTable);

            String[] trafficPoint = {"科技园","南头","同乐","大冲","前海","赤湾","南油","大南山","南山中心区","后海","蛇口","沙河","深圳湾","世界之窗","华侨城","西丽","大学城"};
            for (; matcher.find(); ) {
                for(int j = 0; j < trafficPoint.length; j++) {
                    if(trafficPoint[j].equals(matcher.group(1).replace("\r\n", "").replace(" ", ""))) {

                        temp++;

                        HSSFRow row = sheet.createRow(temp);

                        HSSFCell cell1 = row.createCell(0);

                        cell1.setCellValue(matcher.group(1).replace("\r\n", "").replace(" ", ""));

                        HSSFCell cell2 = row.createCell(1);

                        cell2.setCellValue(matcher.group(2).replace("\r\n", "").replace(" ", ""));

                        HSSFCell cell3 = row.createCell(2);

                        cell3.setCellValue(matcher.group(3).replace("\r\n", "").replace(" ", ""));

                        HSSFCell cell4 = row.createCell(3);

                        cell4.setCellValue(matcher.group(4).replace("\r\n", "").replace(" ", ""));

                        break;
                    }

                }
            }
        }
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        //输出Excel文件
        FileOutputStream output = new FileOutputStream("C:\\Users\\Dell\\Desktop\\Internet\\" + format.format(date) + "-交通数据.xls");
        wb.write(output);
        output.flush();
        output.close();
    }

    public static void main(String[] args) throws Exception {
        GetTrafficData getTrafficData = new GetTrafficData();
        getTrafficData.getTrafficExcel();
    }
}
